/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twodstar.selfie;

import java.util.Arrays;
import java.util.List;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.twodstar.selfie.R;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Fragment_Login extends Fragment 
{
	private static Fragment_Login m_Inst = null;
	
	private Button loginButton;
	private Dialog progressDialog;
	
	public Fragment_Login()
	{
		m_Inst = this;
	}
	
	
	public static Fragment_Login Instance()
	{
		if(m_Inst == null)
			m_Inst = new Fragment_Login();
			
		return m_Inst;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
    	if(getActivity() != null)
    	{
    		((MainActivity)getActivity()).SetLocale();
    	}
    	
        View view = inflater.inflate(R.layout.login, container, false);
        
        loginButton = (Button) view.findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				onLoginButtonClicked();
			}
		});

        return view;
    }
    
    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
    	PBUtility.LOG("test frag act");
		super.onActivityResult(requestCode, resultCode, data);
		ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
	}

	private void onLoginButtonClicked() 
	{
		if(SelfieBattleApplication.Instance().isOnline()== false)
		{
			String text = getString(R.string.check_internet);
	        Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
	        toast.show();
	        return;
		}
		
		progressDialog = ProgressDialog.show(
				getActivity(), "", "Logging in...", true);
		
		List<String> permissions = Arrays.asList(
													"basic_info", 			
													"user_about_me",
													"user_photos",				
													"friends_photos"																							
													);		
	
		
		ParseFacebookUtils.logIn(permissions, getActivity(), new LogInCallback() 
		{
			@Override
			public void done(ParseUser user, ParseException err) 
			{
				progressDialog.dismiss();
				if (user == null) 
				{
					PBUtility.LOG("Uh oh. The user cancelled the Facebook login.");
				} 
				else if (user.isNew()) 
				{
					PBUtility.LOG("User signed up and logged in through Facebook!");
					ShowFragmentCreateNewBattle();
				} 
				else 
				{
					PBUtility.LOG("User logged in through Facebook! " + user.get("FBID"));
					ShowFragmentCreateNewBattle();
					
				}
			}
		});
	}

	private void ShowFragmentCreateNewBattle() 
	{			
		MainActivity Act = (MainActivity)getActivity();
		if(Act != null)
			Act.ShowSelection();
		
	}


	public static void Recreated() 
	{
		// TODO Auto-generated method stub
		m_Inst = null;
	}

    
}
